## Use CMore's mobile api to find movies and tv series
CMORE_API_URL = "https://middleware2.filmnet.se/dispatch-prod/"
module Shin
  module Play
    class Cmore

      def new
        self
      end

      # Fix these before running
      def before(params={})
        raise MissingArgument, "You are missing the argument 'cmore_code' which is required to use this source." unless Shin.get[:cmore_code] != nil

        return "http://www.cmore." + Shin.get[:cmore_code] + "/", CMORE_API_URL + "capiche.cmore." + Shin.get[:cmore_code] + "/"
      end

      # Get a list
      def list(list_id)
        domain, api_domain = before()
        country = Shin.get[:cmore_code]

        # Get list
        response = Base.get(api_domain + 'api/CMA/content/panel/' + list_id + '.json')
        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Can't be nil
        if data != nil
          data['results'].to_hashugar
        else
          raise NotValid, "Couldn't parse the JSON"
        end
      end

      # Show
      def show(params={})
        domain, api_domain = before()
        country = Shin.get[:cmore_code]

        # Response
        response = Base.get(api_domain + 'serie/' + params[:id] + '.json')
        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Can't be nil
        if data != nil
          data.to_hashugar
        else
          raise NotValid, "Couldn't parse the JSON"
        end

      end

      # Movie
      def movie(params={})
        domain, api_domain = before()
        country = Shin.get[:cmore_code]

        # Response
        response = Base.get(api_domain + 'film/' + params[:id] + '.json')
        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Can't be nil
        if data != nil
          data.to_hashugar
        else
          raise NotValid, "Couldn't parse the JSON"
        end

      end

      # Find
      def find(params={})
        domain, api_domain = before()
        country = Shin.get[:cmore_code]

        # Response
        response = Base.get(api_domain + 'films/find.json?page_count=50&' + URI.encode_www_form(params))
        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        # Data
        data = Oj.load(response.body) rescue nil

        # Can't be nil
        if data != nil
          data.to_hashugar
        else
          raise NotValid, "Couldn't parse the JSON"
        end

      end



      # Errors
      class NotValid         < StandardError; end
      class MissingArgument  < StandardError; end
      class HTTPError        < StandardError; end
    end
  end
end
